﻿using System;
using System.Windows;
using System.Threading;
using System.Collections.ObjectModel;

using SimpleMvvmToolkit;

namespace GUIEXAM13
{
    public class BrowseEventsViewModel : ViewModelBase<BrowseEventsViewModel>, IViewModel
    {
        private IViewModelMediator _viewModelMediator;

        public BrowseEventsViewModel(Events eventsModel, IViewModelMediator viewModelMediator)
        {
            EventsModel = eventsModel;
            _viewModelMediator = viewModelMediator;
            _viewModelMediator.Register(this);
        }

        private Events _eventsModel;
        public Events EventsModel
        {
            get { return _eventsModel; }
            set
            {
                _eventsModel = value;
                NotifyPropertyChanged(m => m.EventsModel);
            }
        }

        public ObservableCollection<EventDomain> AllEvents
        {
            get { return EventsModel.AllEvents; }
            set
            {
                EventsModel.AllEvents = value;
                NotifyPropertyChanged(m => m.EventsModel.AllEvents);
            }
        }

        private EventDomain _selectedEvent;
        public EventDomain SelectedEvent
        {
            get { return _selectedEvent; }
            set
            {
                _selectedEvent = value;
                EditEventEnabled = _selectedEvent != null;
                NotifyPropertyChanged(m => m.SelectedEvent);
            }
        }

        public void DeleteSelectedEvent()
        {
            EventsModel.AllEvents.Remove(SelectedEvent);
            EventsModel.SaveAllEvents();
            try
            {
                SelectedEvent = AllEvents[0];
            }
            catch (Exception)
            {
                SelectedEvent = null;
            }
        }

        private bool _editEventEnabled;
        public bool EditEventEnabled
        {
            get { return _editEventEnabled; }
            set
            {
                _editEventEnabled = value;
                NotifyPropertyChanged(m => m.EditEventEnabled);
            }
        }

        public void CreateNewEvent()
        {
            EventsModel.SaveAllEvents();
            var createdEvent = new EventDomain
                                   {
                                       Clothing = new ObservableCollection<ClothingDomain>()
                                   };
            AllEvents.Add(createdEvent);
            SelectedEvent = createdEvent;
            SendEventToCreateEventsView();
        }

        //Mediator stuff
        public void SendEventToCreateEventsView()
        {
            Send("CreateEventViewModel", new ViewModelMessage
                                              {
                                                  Type = ViewModelMessageType.Event,
                                                  Message = SelectedEvent
                                              });
        }

        public string GetName()
        {
            return "BrowseEventsViewModel";
        }

        public void Send(string receiver, ViewModelMessage message)
        {
            _viewModelMediator.Send(GetName(), receiver, message);
        }

        public void Receive(string @from, ViewModelMessage message)
        {
            throw new NotImplementedException();
        }
    }
}